import {defineComponent, h} from "vue";
import {guid} from "@/utils/CommonUtils";

const cacheComs: Map<string, any> = new Map();
// const routerCacheUtils = (Component: any, route: any) => {
//   if (Component) {
//     const wrapperName = route.fullPath;
//     console.log(route.query);
//     if (route?.query?.refresh === 'true') {
//       cacheComs.delete(wrapperName);
//       console.log('delete!!')
//     }
//
//     if (cacheComs.has(wrapperName)) {
//       console.log('缓存')
//       console.log(cacheComs.get(wrapperName))
//       return cacheComs.get(wrapperName);
//     } else {
//       console.log('xinde', Component.type ? Component.type : Component);
//       Component = Component.type ? Component.type : Component;
//       let wrapper = h(Component);
//       cacheComs.set(wrapperName, wrapper);
//       return wrapper;
//     }
//   }
// };

const routerCacheUtils = (Component: any, route: any) => {
  if (Component) {
    const wrapperName = route.fullPath;

    if (cacheComs.has(wrapperName)) {
      console.log('缓存')
      console.log(cacheComs.get(wrapperName));
      return defineComponent({
        name: wrapperName,
        render: () => cacheComs.get(wrapperName)
      });
    } else {
      // Component = Component.type ? Component.type : Component;
      let wrapper:any = h(Component)
      console.log('新的');
      wrapper.cached = 'tang'
      cacheComs.set(wrapperName, wrapper);
      return wrapper;
    }
  }
};



export {routerCacheUtils}